package org.zht.dormpro.dorm.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.model.TbFloor;
import org.zht.dormpro.dorm.service.IFloorService;
import org.zht.dormpro.pub.ResponseMap;

import java.util.*;

@Controller
@RequestMapping("/dorm/floor")
public class FloorController {

    @Autowired
    private IFloorService floorService;

    /**
     * 跳转到楼宇页面
     * @return
     */
    @RequestMapping("/floorPage")
    public String floorPage(){
           return "/floor/floor_list";
    }

    /**
     * 楼宇页面
     * @param params
     * @return
     */
    @RequestMapping("/listFloor")
    @ResponseBody
    @RequiresPermissions("dorm:list:all")
    public ResponseMap listFloor(@RequestParam Map<String,Object> params){
        PageBean pageBean = new PageBean(params);
        List<TbFloor> floors = floorService.getFloorByCondition(pageBean);
        Map<String,Object> attr = new HashMap<>();
        attr.put("total",pageBean.getTotal());
        return ResponseMap.success(floors,attr);
    }

    /**
     * 跳转到页面编辑
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/floorEditPage/{id}")
    public String floorEditPage(Model model,@PathVariable("id") Integer id){
        TbFloor floor = floorService.getFloorById(id);
        model.addAttribute("floor",floor);

        return "/floor/floor_edit";
    }

    /**
     * 修改楼宇信息
     * @param floor
     * @return
     */
    @RequestMapping(value = "/editFloor",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap editFloor(TbFloor floor){
        boolean success = floorService.editFloor(floor);
        if(success){
            return ResponseMap.success("修改楼宇成功");
        }else {
            return ResponseMap.error("修改楼宇失败");
        }
    }

    /**
     * 跳转到添加楼宇页面
     * @return
     */
    @RequestMapping("/addFloorPage")
    public String addFloorPage(){

        return "/floor/floor_add";
    }

    /**
     * 添加楼宇信息
     * @param floor
     * @return
     */
    @RequestMapping(value = "/addFloor",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap addFloor(TbFloor floor){
        boolean success = floorService.addFloor(floor);
        if(success){
            return ResponseMap.success("楼宇添加成功");
        }else {
            return ResponseMap.error("楼宇添加失败");
        }
    }

    /**
     * 删除楼宇
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteFloor",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap deleteFloor(Integer id){
        boolean success = floorService.deleteFloor(id);
        if(success){
            return ResponseMap.success("楼宇删除成功");
        }else {
            return ResponseMap.error("楼宇删除失败");
        }
    }

    /**
     * 批量删除楼宇
     * @param floorIds
     * @return
     */
    @RequestMapping(value = "/batchRemoveFloor",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap batchRemoveFloor(@RequestParam("floorIds[]")String[] floorIds){
        boolean success = floorService.batchRemoveFloor(new ArrayList<String>(Arrays.asList(floorIds)));
        if (success){
            return ResponseMap.success("批量删除成功");
        }else {
            return ResponseMap.error("批量删除失败");
        }
    }
}

